// pages/login/login.js
import {userstore} from '../../store/userstore'
import {setstorage,getstorage} from '../../utils/storage'
import {ComponentWithStore} from 'mobx-miniprogram-bindings'
import {requserdata,requserinfo} from '../../api/user'
ComponentWithStore({
  data:{

  },
  methods:{
    login(){
      wx.login({
        success: async({code}) => {
          const res=await requserdata(code)
          // 将token存储到本地以及userstore
          setstorage("token",res.data.token)
          userstore.settoken(res.data.token)
          // 获取用户信息,并存储到本地以及userstore
          const {data}= await requserinfo(code)
          setstorage("userinfo",data.userinfo)
          userstore.setuserinfo(data.userinfo)
          wx.navigateBack()
        },
      })
    }
  },
  storeBindings:{
    store:userstore,
    fields:['token'],
    actions:['settoken']
  }
})
